package com.qxzn.module.mine.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.OtherFamilyMemberBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.widget.ToastUtil;
import com.qxzn.app.util.CommonUtil;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.module.mine.R;
import com.qxzn.module.mine.R2;

import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ModifyPhoneActivity extends BaseActivity implements TextWatcher, View.OnTouchListener {
    @BindView(R2.id.modify_phone_content)
    AppCompatEditText editText;
    @BindView(R2.id.btn_next)
    Button btn_next;

    @Override
    protected int initLayout() {
        return R.layout.activity_modify_phone;
    }

    @Override
    protected void initView() {
        editText.addTextChangedListener(this);
        btn_next.setOnClickListener(this);
        editText.setOnTouchListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (CommonUtil.isFastClick()) return;
        String phoneNum = editText.getText().toString().trim();
        if (view.getId() == R.id.btn_next) {
            if (TextUtils.equals(SharePreferenceUtil.getPhone(this), phoneNum)) {
                ToastUtil.showTextToast(this, "不用转让给自己哦");
            } else {
                JSONObject params = new JSONObject();
                params.put("device_id", SharePreferenceUtil.getDeviceId(this));
                params.put("username", SharePreferenceUtil.getUserName(this));
                HttpRequest.getService().getMembers(HttpRequest.getDefaultBody(params)).enqueue(new Callback<BaseBean<List<OtherFamilyMemberBean>>>() {
                    @Override
                    public void onResponse(Call<BaseBean<List<OtherFamilyMemberBean>>> call, Response<BaseBean<List<OtherFamilyMemberBean>>> response) {
                        hideProgress();
                        BaseBean<List<OtherFamilyMemberBean>> baseBean = response.body();
                        if (baseBean.getCode() == 200) {
                            List<OtherFamilyMemberBean> data = baseBean.getData();
                            boolean ok = false;
                            if (data != null) {
                                for (OtherFamilyMemberBean bean : data) {
                                    if (TextUtils.equals(phoneNum, bean.mobile)) {
                                        ok = true;
                                    }
                                }
                            }
                            if (ok) {
                                startActivity(new Intent(ModifyPhoneActivity.this, VerificationCode.class)
                                        .putExtra(VerificationCode.EXTRA_TITLE_SUB, getString(R.string.modify_admin_title))
                                        .putExtra(VerificationCode.EXTRA_NAME_PROMPT, "新管理员")
                                        .putExtra(VerificationCode.EXTRA_PHONE, phoneNum)
                                        .putExtra(VerificationCode.EXTRA_TYPE_int, 6)
                                        .putExtra(VerificationCode.EXTRA_ACTIVITY, "com.qxzn.module.mine.activity.ModifyPhoneCompleteActivity"));
                            } else {
                                ToastUtil.showTextToast(ModifyPhoneActivity.this, "只能转让管理权给家庭成员哦");
                            }
                            return;
                        }
                        ToastUtil.showTextToast(ModifyPhoneActivity.this, baseBean.getMsg());
                    }

                    @Override
                    public void onFailure(Call<BaseBean<List<OtherFamilyMemberBean>>> call, Throwable t) {
                        hideProgress();
                        ToastUtil.showTextToast(ModifyPhoneActivity.this, getString(R.string.network_error));
                    }
                });
            }
        }
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText(R.string.modify_admin_title);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.length() == 11) {
            String phone = s.toString();
            if (CommonUtil.isMobile(phone)) {
                btn_next.setEnabled(true);
                return;
            } else {
                ToastUtil.showTextToast(this, "手机号格式有误");
            }
        }
        btn_next.setEnabled(false);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // et.getCompoundDrawables()得到一个长度为4的数组，分别表示左右上下四张图片
        Drawable drawable = editText.getCompoundDrawables()[2];
        //如果右边没有图片，不再处理
        if (drawable == null)
            return false;
        //如果不是按下事件，不再处理
        if (event.getAction() != MotionEvent.ACTION_UP)
            return false;
        if (event.getX() > editText.getWidth()
                - editText.getPaddingRight()
                - drawable.getIntrinsicWidth()) {
            editText.setText("");
        }
        return false;
    }
}
